﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Thaire.Investment.Common;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class InstructionFormModel
    {

        public InstructionFormModel()
        {
            InitData();
        }
       
        public void SetData(InstructionFormScreenData scrdata)
        {
            InstructionFormScreenDataCache = scrdata;



            foreach (var item in scrdata.CompanyList)
            {
                CompanyList.Add(new SelectListItem() { Text = item.CompanyNameTh, Value = item.CompanyId.ToString() });
            }
            foreach (var item in scrdata.FinancialInstrumentList)
            {
                FinancialInstrumentList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ID.ToString() });
            }


          
            //FinancialInstrumentList.Add(new SelectListItem() { Text = "Fixed Income Securities", Value = "2" });


            if (InstructionFormScreenDataCache.Header != null)
            {
                To = InstructionFormScreenDataCache.Header.TO;
                Attn = InstructionFormScreenDataCache.Header.Attention;
                Email = InstructionFormScreenDataCache.Header.Mail;
                //  CompanyList = new BindingList<SelectListItem>();
                //  TransactionDataGridAdapterList = new List<TransactionDataGridAdapter>();
                // SelectedTransactionDataGridAdapterList = new List<TransactionDataGridAdapter>();
                // FinancialInstrumentList = new BindingList<SelectListItem>();

                //  FinancialInstrumentList = WebData.FinancialInstrument;
                // CompanyList.Insert(0, WebUtil.SelectListSelectDefault());
                // FinancialInstrumentList.Insert(0, WebUtil.SelectListSelectDefault());
                Status = InstructionFormScreenDataCache.Header.Status + "";
                //  Email = null;
                FinancialInstrument = InstructionFormScreenDataCache.Header.FinancialInstrumentID.ToString();
                Company = InstructionFormScreenDataCache.Header.CompanyID.ToString();
                ComTelNo = InstructionFormScreenDataCache.Header.CompanyTel;
                ComFaxNo = InstructionFormScreenDataCache.Header.CompanyFax;
                ComEmail = InstructionFormScreenDataCache.Header.CompanyMail;
                TradeDate = InstructionFormScreenDataCache.Header.TradeDate;
                SettlementDate = InstructionFormScreenDataCache.Header.SettlementDate;
                if (InstructionFormScreenDataCache.Header.AssetClassID.HasValue)
                    FinancialInstrumentAssetClass = InstructionFormScreenDataCache.Header.AssetClassID.ToString();

                foreach (var item in scrdata.AssetClassList)
                {
                    if (item.FinancialInstrumentsReferenceCodeID == InstructionFormScreenDataCache.Header.FinancialInstrumentID.GetValueOrDefault())
                        FinancialInstrumentAssetClassList.Add(new SelectListItem() { Text = item.AssetClassEN, Value = item.AssetClassID.ToString() });
                }
            }
           


            int Key = 0;
            TransactionDataGridAdapterList.Clear();
            SelectedTransactionDataGridAdapterList.Clear();
            if (InstructionFormScreenDataCache.InstructionDataDTO != null)
            {
                foreach (var item in InstructionFormScreenDataCache.InstructionDataDTO)
                {
                    TransactionDataGridAdapter adapter = new TransactionDataGridAdapter(item);
                    adapter.Key = Key;
                    SelectedTransactionDataGridAdapterList.Add(adapter);
                    Key++;
                }
                InstrumentType = Thaire.Investment.Common.InstrumentType.FixedIncome;
            }
            if (InstructionFormScreenDataCache.InstructionUnitTrustGridDetail != null)
            {
                foreach (var item in InstructionFormScreenDataCache.InstructionUnitTrustGridDetail)
                {
                    TransactionDataGridAdapter adapter = new TransactionDataGridAdapter(item);
                    adapter.Key = Key;
                    SelectedTransactionDataGridAdapterList.Add(adapter);
                    Key++;
                }
                InstrumentType = Thaire.Investment.Common.InstrumentType.UnitTrust;
            }
            if (InstructionFormScreenDataCache.InstructionEquityGridDetail != null)
            {
                foreach (var item in InstructionFormScreenDataCache.InstructionEquityGridDetail)
                {
                    TransactionDataGridAdapter adapter = new TransactionDataGridAdapter(item);
                    adapter.Key = Key;
                    SelectedTransactionDataGridAdapterList.Add(adapter);
                    Key++;
                }
                InstrumentType = Thaire.Investment.Common.InstrumentType.Equity;
            }

            if (InstructionFormScreenDataCache.InstructionDepositAtFinancialGridDetail != null)
            {
                foreach (var item in InstructionFormScreenDataCache.InstructionDepositAtFinancialGridDetail)
                {
                    TransactionDataGridAdapter adapter = new TransactionDataGridAdapter(item);
                    adapter.Key = Key;
                    SelectedTransactionDataGridAdapterList.Add(adapter);
                    Key++;
                }
                InstrumentType = Thaire.Investment.Common.InstrumentType.DepositAtFinancial;
            }
            //foreach (var item in scrdata.AssetClassList)
            //{
            //    model.CompanyList.Add(new SelectListItem() { Text = item.CompanyNameTh, Value = item.CompanyId.ToString() });
            //}
            //return model;
        }

        public void InitData()
        {
           
            To = ConfigurationManager.AppSettings["DefaultInstructionTo"];
            Attn = ConfigurationManager.AppSettings["DefaultInstructionAttn"];
            Email = ConfigurationManager.AppSettings["DefaultInstructionEMail"];
            CompanyList = new BindingList<SelectListItem>();
            TransactionDataGridAdapterList = new List<TransactionDataGridAdapter>();
            SelectedTransactionDataGridAdapterList = new List<TransactionDataGridAdapter>();
             FinancialInstrumentList = new BindingList<SelectListItem>();
             FinancialInstrumentAssetClassList = new BindingList<SelectListItem>();
          //  FinancialInstrumentList = WebData.FinancialInstrument;
             CompanyList.Insert(0, WebUtil.SelectListSelectDefault());
            FinancialInstrumentList.Insert(0, WebUtil.SelectListSelectDefault());
            FinancialInstrumentAssetClassList.Insert(0, WebUtil.SelectListSelectDefault());
            Status = null;
            //  Email = null;
            Company = null;
            ComTelNo = null;
            ComFaxNo = null;
            ComEmail = null;
            TradeDate = null;
            SettlementDate = null;
            FinancialInstrumentAssetClass = null;
        }
        public InstrumentType InstrumentType { set; get; }
        public String[] Selected { set; get; }

        public String To { set; get; }

        public String Attn { set; get; }
        public String Status { set; get; }
        public String Email { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public String Company { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public String FinancialInstrument { set; get; }

          [Required(ErrorMessage = "โปรดระบุ")]
        public String FinancialInstrumentAssetClass { set; get; }

        public String ComTelNo { set; get; }

         [Required(ErrorMessage = "โปรดระบุ")]
        public DateTime? TradeDate { set; get; }
        public String ComFaxNo { set; get; }

         [Required(ErrorMessage = "โปรดระบุ")]
        public DateTime? SettlementDate { set; get; }
        public String ComEmail { set; get; }

        public String BtnGetSelection { set; get; }
        public String BtnSave { set; get; }
        public String BtnAdd { set; get; }
        public String BtnExportExcel { set; get; }
        public String BtnExportExcelInitial { set; get; }
        public String DeleteInstructionKey { set; get; }
        public bool Updatemode { set; get; }

        public IList<SelectListItem> CompanyList { set; get; }
        public IList<SelectListItem> FinancialInstrumentList { set; get; }
        public IList<SelectListItem> FinancialInstrumentAssetClassList { set; get; }
        public IList<TransactionDataGridAdapter> TransactionDataGridAdapterList { set; get; }

        public IList<TransactionDataGridAdapter> SelectedTransactionDataGridAdapterList { set; get; }


        public InstructionFormScreenData InstructionFormScreenDataCache { set; get; }
        public InstructionFormScreenData ToInstructionFormScreenData()
        {
            InstructionFormScreenData scrdata = new InstructionFormScreenData();
            scrdata.Attn = Attn;
            scrdata.ComEmail = ComEmail;
            scrdata.ComFaxNo = ComFaxNo;
            scrdata.Company = Company;
            scrdata.ComTelNo = ComTelNo;
            scrdata.Email = Email;
            scrdata.FinancialInstrument = FinancialInstrument;
            scrdata.SettlementDate = SettlementDate;
            scrdata.Status = Status;
            scrdata.To = To;
            scrdata.TradeDate = TradeDate;

            return scrdata;
        }



    }
}